/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

package com.huawei.signature.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.core.http.HttpConfig;
import com.huaweicloud.sdk.core.region.Region;
import com.huaweicloud.sdk.msgsms.v2.MsgsmsClient;
import com.huaweicloud.sdk.msgsms.v2.model.CreateSignatureRequest;
import com.huaweicloud.sdk.msgsms.v2.model.CreateSignatureResponse;
import com.huaweicloud.sdk.msgsms.v2.model.DeleteSignatureRequest;
import com.huaweicloud.sdk.msgsms.v2.model.DeleteSignatureResponse;
import com.huaweicloud.sdk.msgsms.v2.model.ShowSignatureRequest;
import com.huaweicloud.sdk.msgsms.v2.model.ShowSignatureResponse;
import com.huaweicloud.sdk.msgsms.v2.model.SmsSignatureReq;
import com.huaweicloud.sdk.msgsms.v2.model.UploadSignatureFileRequest;
import com.huaweicloud.sdk.msgsms.v2.model.UploadSignatureFileRequestBody;
import com.huaweicloud.sdk.msgsms.v2.model.UploadSignatureFileResponse;
import com.huaweicloud.sdk.msgsms.v2.region.MsgsmsRegion;

/**
 * 提供创建短信签名、获取签名详情、删除短信签名样例，包含创建短信签名时需的上传签名文件样例。
 */
public class MsgsmsSignatureManageDemo {
    public static void main(String[] args) {
        System.out.println("Start HUAWEI CLOUD MSGSMS Signature Manage Java Demo...");

        ICredential auth = getCredential("YOUR AK", "YOUR SK","YOUR PROJECT ID");

        // 创建MsgsmsClient实例
        MsgsmsClient client = getClient(MsgsmsRegion.CN_NORTH_4, auth);

        try {
            String fileId = uploadSignatureFile(client);
            String id = createSignature(client, "YOUR APPID", fileId);
            showSignature(client, id);
            deleteSignature(client, id);
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println(e.getMessage());
        } catch (ServiceResponseException e) {
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }

    /**
     * 上传申请文件
     * @param client MsgsmsClient实例
     * @return 签名文件ID
     */
    private static String uploadSignatureFile(MsgsmsClient client) {
        UploadSignatureFileRequest request = new UploadSignatureFileRequest();
        request.setFileDesc("test");

        UploadSignatureFileRequestBody fileRequestBody = new UploadSignatureFileRequestBody();
        fileRequestBody.withFile(
                MsgsmsSignatureManageDemo.class.getClassLoader().getResourceAsStream("signature_test_file.png"),
                "signature_test_file.png");
        request.setBody(fileRequestBody);

        // 调用SDK的接口上传申请文件
        UploadSignatureFileResponse response = client.uploadSignatureFile(request);
        System.out.println(response.toString());
        return response.getFileId();
    }

    /**
     * 创建短信签名
     * @param client MsgsmsClient实例
     * @param appId 应用ID
     * @param fileId 签名文件ID
     * @return 签名主键ID
     */
    private static String createSignature(MsgsmsClient client, String appId, String fileId) {
        CreateSignatureRequest request = new CreateSignatureRequest();
        SmsSignatureReq body = new SmsSignatureReq();
        body.setAppId(appId);
        body.setSignatureName("myTestSignature");
        body.setSignatureSource(0);
        body.setSignatureType("VERIFY_CODE_TYPE");
        body.setApplyDesc("my test signature apply desc.");
        body.setSourceTitleContent("Test");
        body.setIsInvolvedThird("No");
        body.setFileId(fileId);
        request.withBody(body);

        // 调用SDK的接口创建短信签名
        CreateSignatureResponse response = client.createSignature(request);
        System.out.println(response.toString());
        return response.getId();
    }

    /**
     * 获取签名详情
     * @param client MsgsmsClient实例
     * @param id 签名主键ID
     */
    private static void showSignature(MsgsmsClient client, String id) {
        ShowSignatureRequest request = new ShowSignatureRequest();
        request.setId(id);

        // 调用SDK的接口获取签名详情
        ShowSignatureResponse response = client.showSignature(request);
        System.out.println(response.toString());
    }

    /**
     * 删除短信签名
     * @param client MsgsmsClient实例
     * @param id 签名主键ID
     */
    private static void deleteSignature(MsgsmsClient client, String id) {
        DeleteSignatureRequest request = new DeleteSignatureRequest();
        request.setId(id);

        // 调用SDK的接口删除短信签名
        DeleteSignatureResponse response = client.deleteSignature(request);
        System.out.println(response.toString());
    }

    /**
     * 创建鉴权凭证
     * @param ak 租户账号对应的Access Key (AK)
     * @param sk 租户账号对应的Secret Access Key (SK)
     * @param projectId 租户账号对应的IAM项目ID
     * @return 鉴权凭证
     */
    public static ICredential getCredential(String ak, String sk, String projectId) {
        return new BasicCredentials()
                .withAk(ak)
                .withSk(sk)
                .withProjectId(projectId);
    }

    /**
     * 创建MsgsmsClient实例
     * @param region region信息
     * @param auth 鉴权凭证
     * @return MsgsmsClient实例
     */
    public static MsgsmsClient getClient(Region region, ICredential auth) {
        // 使用默认配置
        HttpConfig config = HttpConfig.getDefaultHttpConfig();
        config.withIgnoreSSLVerification(true);

        // 初始化Msgsms服务的客户端
        return MsgsmsClient.newBuilder()
                .withHttpConfig(config)
                .withCredential(auth)
                .withRegion(region)
                .build();
    }
}
